/**
 *
 */
package com.help10000.rms.common;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

/**
 * @author justKeepmoving
 * @date 2015年7月13日下午1:29:37
 */
public abstract class BaseDialogFragment extends DialogFragment {

    protected FragmentActivity activity;
    protected View view;
    protected boolean isLazied, isResumed;


    public String getFragmentTag() {
        return getClass().getName();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (AppCompatActivity) activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayout(), container, false);
        preInit(savedInstanceState);
        initUi();
        initListener();
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDialogAttribute(getDialog());
    }

    private void initWindowAttribute(Dialog dialog) {
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
    }

    protected void executeOnUiThread(Runnable runnable) {
        activity.runOnUiThread(runnable);
    }

    /**
     * 初始化windows相关属性，
     */
    protected void initDialogAttribute(Dialog dialog) {
        initWindowAttribute(dialog);

    }

    protected void preInit(Bundle savedInstanceState) {
    }

    protected void initUi() {

    }

    protected void initListener() {

    }

    /**
     * dialog资源文件
     * @return
     */
    protected abstract int getLayout();


    protected void onVisible() {
        if (isResumed && !isLazied) {
            isLazied = true;
            lazyLoad();
        }
    }

    protected void lazyLoad() {

    }


    @Override
    public void onResume() {
        super.onResume();
        isResumed = true;
        if (!isLazied) {
            onVisible();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        isResumed = false;
    }

    /**
     * 页面统计名称
     * @return
     */
    protected String getPageName() {
        return getClass().getSimpleName();
    }


    @Override
    public void onDestroyView() {
        isResumed = false;

        super.onDestroyView();
    }


}
